package cn.ljy.mes.service.impl;

import cn.ljy.authority.service.impl.IBaseService;
import cn.ljy.common.exception.model.MyServiceException;
import cn.ljy.common.exception.model.MyWebException;
import cn.ljy.mes.dao.mapper.*;
import cn.ljy.mes.model.WmsWarehouseSeat;
import cn.ljy.mes.model.query.WmsMaterialInventoryDetailQuery;
import cn.ljy.mes.model.query.WmsMaterialInventoryDetailRelocationLogQuery;
import cn.ljy.mes.model.query.WmsWarehouseSeatQuery;
import cn.ljy.mes.service.WmsWarehouseSeatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.io.Serializable;

@Service
//@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor= MyRollBackException.class)
@Validated//表示开启sprint的校检框架，会自动扫描方法里的@Valid（@Valid注解一般写在接口即可）
@Slf4j
public class IWmsWarehouseSeatService extends IBaseService<WmsWarehouseSeat, WmsWarehouseSeatQuery> implements WmsWarehouseSeatService {
    @Autowired
    private WmsWarehouseMapper wmsWarehouseMapper;
    @Autowired
    private WmsWarehouseAreaMapper wmsWarehouseAreaMapper;
    @Autowired
    private WmsMaterialInventoryMapper materialInventoryMapper;
    @Autowired
    private WmsMaterialInventoryDetailMapper materialInventoryDetailMapper;
    @Autowired
    private WmsMaterialInventoryDetailRelocationLogMapper materialInventoryDetailRelocationLogMapper;


    @Override
    protected void enrichEntityFields(WmsWarehouseSeat wmsWarehouseSeat){
        if(wmsWarehouseSeat!=null){
            //获取库位被几个物料占用
            WmsMaterialInventoryDetailQuery materialInventoryDetailQuery=new WmsMaterialInventoryDetailQuery();
            materialInventoryDetailQuery.setWarehouseSeat(wmsWarehouseSeat);
            int materialInventoryDetailNumber=materialInventoryDetailMapper.count(materialInventoryDetailQuery);
            wmsWarehouseSeat.setMaterialInventoryDetailNumber(materialInventoryDetailNumber);
        }
    }

    @Override
    protected void beforeSave(WmsWarehouseSeat wmsWarehouseSeat) {
        if(wmsWarehouseSeat.getWarehouseArea()==null||wmsWarehouseSeat.getWarehouseArea().getId()==null){
            throw new MyWebException("验证失败：请先选择所属库区");
        }
    }

    @Override
    protected void beforeUpdate(WmsWarehouseSeat wmsWarehouseSeat) {
        //库区不是必须要传递，但如果传递了id就不能为空
        if(wmsWarehouseSeat.getWarehouseArea()!=null&&wmsWarehouseSeat.getWarehouseArea().getId()==null){
            throw new MyWebException("验证失败：请先选择所属库区");
        }
    }

    @Override
    protected void beforeDelete(Serializable[] ids) {
        for(Serializable id:ids){
            if(id!=null){
                WmsWarehouseSeat seat=mapper.findById(id);
                if(seat==null){
                    continue;//如果不存在说明已经删除，不在弹出业务信息。
                }
                //1.先判断库位是否已经使用
                WmsMaterialInventoryDetailQuery materialInventoryDetailQuery=new WmsMaterialInventoryDetailQuery();
                materialInventoryDetailQuery.setWarehouseSeat(new WmsWarehouseSeat(id.toString()));
                int materialInventoryDetailNumber=materialInventoryDetailMapper.count(materialInventoryDetailQuery);
                if(materialInventoryDetailNumber>0){
                    throw new MyServiceException(String.format("删除失败：库位%s正被%d种物料占用，无法删除！",seat.getName(),materialInventoryDetailNumber));
                }
                //2.先判断库位是否已经有使用的日志，即使当前已经没有物料在库位中，也不允许删除库位信息
                //2.1 判断源库位
                WmsMaterialInventoryDetailRelocationLogQuery materialInventoryDetailRelocationLogQuery=new WmsMaterialInventoryDetailRelocationLogQuery();
                materialInventoryDetailRelocationLogQuery.setWarehouseSeatOfSource(new WmsWarehouseSeat(id.toString()));
                int materialInventoryDetailRelocationLogNumber=materialInventoryDetailRelocationLogMapper.count(materialInventoryDetailRelocationLogQuery);
                if(materialInventoryDetailRelocationLogNumber>0){
                    throw new MyServiceException(String.format("删除失败：库位%s虽然已经没有物料在使用，但是已经有出库的使用记录，无法删除！",seat.getName()));
                }
                //2.2 判断目标库位
                materialInventoryDetailRelocationLogQuery=new WmsMaterialInventoryDetailRelocationLogQuery();
                materialInventoryDetailRelocationLogQuery.setWarehouseSeatOfTarget(new WmsWarehouseSeat(id.toString()));
                materialInventoryDetailRelocationLogNumber=materialInventoryDetailRelocationLogMapper.count(materialInventoryDetailRelocationLogQuery);
                if(materialInventoryDetailRelocationLogNumber>0){
                    throw new MyServiceException(String.format("删除失败：库位%s虽然已经没有物料在使用，但是已经有入库的使用记录，无法删除！",seat.getName()));
                }
            }
        }
    }

}
